//
//  RW_SongModel.m
//  SleepApp
//
//  Created by MAC on 2021/3/25.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_SongModel.h"
#import "FBLPromises.h"
#import "NSFileManager+WLCache.h"
#import "NSFileManager+WLExtension.h"

@implementation RW_SongModel




#pragma mark -- 数据获取
/** 下载音频文件 */
-(void)downloadAudioProgress:(void(^)(float progress))progress
                     success:(void(^)(NSString *filePath))success
                        fail:(void(^)(NSError *error))fail{
    //获取音频缓存路径
    NSString *audioPath = [NSFileManager readKeyForPath_wl:[self.url lastPathComponent] folderName:@"Audio" dataType:defaultData_wl];
    if (audioPath){
        if (success) {
            success(audioPath);
        }
        return;
    }
    
    [HTTPRequestTool downloadWithUrl:self.url progress:^(int64_t bytesRead, int64_t totalBytesRead) {
        if (progress) {
            progress((float)bytesRead/(float)totalBytesRead);
        }
    } success:^(id response) {
        NSURL *pathUrl = [NSURL URLWithString:response];
        NSData *fileData = [NSData dataWithContentsOfURL:pathUrl];
        NSString *savePath = [NSFileManager saveData_wl:fileData key:[self.url lastPathComponent] folderName:@"Audio" dataType:defaultData_wl];
        if (savePath){
            [NSFileManager wl_deleteFilePath:[pathUrl path]];
            if (success) {
                success(savePath);
            }
        } else {
            if (fail){
                fail([NSError errorWithDomain:@"文件保存失败" code:999 userInfo:nil]);
            }
        }
    } failure:fail];
}
/** 获取歌曲详情 */
//+(void)getSongInfoId:(NSString *)xid
//             success:(void(^)(RW_SongModel *response))success
//                fail:(void(^)(NSError *error))fail{
//    [[[self getSongInfoId:xid] then:^id(RW_SongModel *value) {
//        if (success){
//            success(value);
//        }
//        return nil;
//    }] catch:fail];
//    //    NSMutableArray *arr = [NSMutableArray new];
//    //    [arr addObject:[self getSongInfoId:xid]];
//    //
//    //    [[[FBLPromise all:arr] then:^id _Nullable(NSArray * _Nullable value) {
//    //        if (success){
//    //            success(value);
//    //        }
//    //        return value;
//    //    }] catch:^(NSError * _Nonnull error) {
//    //        if (fail){
//    //            fail(error);
//    //        }
//    //    }];
//}



#pragma mark -- 网络请求
/**
 歌曲详情获取
 */
+ (FBLPromise<RW_SongModel *> *)getSongInfoId:(NSString *)songId {
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        NSLog(@"推荐分类-好书推荐类别获取");
        [HTTPRequestTool getWithUrl:getSongInfoUrl refreshCache:NO params:@{
            @"xid":songId
        } success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            RW_SongModel *m = [RW_SongModel yy_modelWithJSON:model.data];
            fulfill(m);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}
@end
